<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$resultat = (!empty($_REQUEST['resultat'])) ? $_REQUEST['resultat'] : "";
$match = (!empty($_REQUEST['match'])) ? $_REQUEST['match'] : "";
if($resultat && $match){
	$match = new match($match);
	$equipe = new equipe($match->equipe,"nom");
	if($equipe->est_membre(check_cookie() || check_admin())){
		$match->resultat = $resultat;
		$match->update();
		echo "ok";
	}
	else echo "erreur";
}
elseif(check_admin()){
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	$all = (!empty($_REQUEST['all'])) ? $_REQUEST['all'] : "";	
	$table_row_id = (!empty($_REQUEST['table_row_id'])) ? $_REQUEST['table_row_id'] : 0;
	$equipe_id = (!empty($_REQUEST['equipe_id'])) ? $_REQUEST['equipe_id'] : "";	
	if($action == 'form_new'){
		$match = new match('');
		echo $match->form_edit($equipe_id);
	}
	elseif($action == 'del'){
		$match = new match($id);
		echo ($match->delete()) ? 1 : 0;
	}
	elseif($action == 'resultat'){
		$resultat = (!empty($_REQUEST['resultat'])) ? $_REQUEST['resultat'] : "";
		$match = new match($id);
		$match->resultat = $resultat;
		echo ($match->update()) ? "<span class = 'ok'>ok</span>" : "<span class = 'ko'>erreur</span>";
	}
	else{
		$variables = array("no","date","heure","resultat","salle_id","marqueur1_id","marqueur2_id","local","visiteur","equipe","marquage");
		foreach($variables as $var){
			$$var = (!empty($_REQUEST[$var])) ? $_REQUEST[$var] : "";
			if($var == "date"){
				if(ereg("([0-9]{2})[\.-]([0-9]{2})[\.-]([0-9]{4})",$date,$regs)){
					$date = $regs[3]."-".$regs[2]."-".$regs[1];
				}
				elseif(ereg("([0-9]{4})[\.-]([0-9]{2})[\.-]([0-9]{2})",$date,$regs)){
					$date = $date;
				}
				else{ $date = ""; $jour = "";}
			}
			elseif($var == 'heure'){
				if(ereg("([0-9]{1,2}):([0-9]{2}):([0-9]{2})",$heure,$regs)){
					$heure = $heure;
				}
				elseif(ereg("([0-9]{1,2})[:h]([0-9]{2})",$heure,$regs)){
					$heure = $regs[1].":".$regs[2].":00";
				}
				else $heure = "";
			}
			elseif($var == 'resultat'){
				if(ereg("^([0-3]).([0-3])$",$resultat,$regs)){
					$resultat = $regs[1]."-".$regs[2];
				}
				else $resultat = "";
			}
			elseif($var == 'salle_id'){
				$obj_salle = new salle($salle_id);
				if(!$obj_salle->no){
					$nom_salle = "";
					$salle = "";
				} 
				else {
					$nom_salle = str_ireplace("salle","",$obj_salle->nom);
					$salle = $obj_salle->no;
				}
			}
			elseif($var == 'marqueur1_id'){
				if($$var){
					$marqueur = new membre($$var);
					$marqueur1 = ($marqueur->marqueur == 0) ? "" : $$var;
				}
			}
			elseif($var == 'marqueur2_id'){
				if($$var){
					$marqueur = new membre($$var);
					$marqueur2 = ($marqueur->marqueur == 0) ? "" : $$var;
				}
			}

		}
		$match = new match($no);
		if($match->no){
			foreach($variables as $var){
				if(strpos($var,"_id") !== FALSE) $var = substr($var,0,-3);
				$match->$var = $$var;
			}
			$result = $match->update();
#			echo format_heure($match->heure)."<!--!>";		
#			echo format_date($match->date)."<!--!>";
#			echo $nom_salle."<!--!>";
#			echo $match->format_resultat()."<!--!>";
			echo "Modification effectu&eacute;e avec succ&eacute;s";
			if($result > 1){
				
				$fields = array("jour","date","heure","salle","local","visiteur","resultat");
				foreach($fields as $field){
					if($field == 'date') $print = format_date($match->date);
					elseif($field == 'heure') $print = format_heure($match->heure);
					elseif($field == 'salle') $print = $nom_salle;
					elseif($field == 'resultat') $print = $match->format_resultat();
					else $print = $match->$field;
					echo "<script type = 'text/javascript'>if(document.getElementById('_".$table_row_id."_$field')) document.getElementById('_".$table_row_id."_$field').innerHTML='".(addslashes($print))."'</script>";				
				}				
			}
			else{
				if($all){
					echo "<script src = 'javascripts/vbcll.js' type = 'text/javascript'></script>";
					echo "<script type = 'text/javascript'>";
					echo "text = file('calendrier_admin.php?action=print');";
					echo "document.getElementById('calendrier').innerHTML=text;";
					echo "</script>";						
				}
				else{
					$equipe = new equipe($match->equipe,"nom");
					echo "<script src = 'javascripts/vbcll.js' type = 'text/javascript'></script>";
					echo "<script type = 'text/javascript'>";
					echo "text = file('equipes_admin.php?id=$equipe->id&action=calendrier');";
					echo "document.getElementById('calendrier').innerHTML=text;";
					echo "</script>";	
				}
				
			}

		}		
	}
	
}

?>
